package pers.corvey.studyjava.model.form;

import java.util.ArrayList;
import java.util.List;

import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import pers.corvey.studyjava.model.Choice;
import pers.corvey.studyjava.model.Question;
import pers.corvey.studyjava.util.ToStringUtils;

public class QuestionForm implements MyValidatable {

    @NotNull(message="题干不能为空！")
    @Size(min=1, message="题干不能为空！")
    private String questionContent;
    
    @NotNull(message="选项不能为空！")
    @Size(min=2, message="选项至少需要两个！")
    private List<String> choicesContent;
    
    @NotNull(message="答案不能为空！")
    private List<Boolean> corrects;
    
    @AssertTrue(message="选项不能为空！")
    private boolean choicesNotNull = true;
    
    @AssertTrue(message="选项与其正确性必须一一对应")
    private boolean choiceCorrectSameNum;
    
    @Min(value=1, message="至少要有一个正确选项！")
    private int correctCount = 0;
    
    public QuestionForm() {}

    public Question toQuestion() {
        Question question = new Question();
        question.setContent(getQuestionContent());
        question.setType(correctCount == 1 ? 1 : 2);
        List<Choice> choices = new ArrayList<>();
        for (int i=0; i<getChoicesContent().size(); ++i) {
            Choice choice = new Choice();
            choice.setContent(getChoicesContent().get(i));
            choice.setCorrect(getCorrects().get(i));
            choices.add(choice);
        }
        question.setChoices(choices);
        return question;
    }
    
    @Override
    public void validate() {
        choiceCorrectSameNum = choicesContent.size() == corrects.size();
        MyValidatable.super.validate();
    }
    
    public String getQuestionContent() {
        return questionContent;
    }

    public void setQuestionContent(String questionContent) {
        this.questionContent = questionContent.trim();
    }

    public List<String> getChoicesContent() {
        return choicesContent;
    }

    public void setChoicesContent(List<String> choicesContent) {
        this.choicesContent = new ArrayList<String>();
        if (choicesContent != null) {
            for (String content : choicesContent) {
                if (content == null) {
                    choicesNotNull = false;
                }
                content = content.trim();
                if ("".equals(content)) {
                    choicesNotNull = false;
                } else {
                    this.choicesContent.add(content);
                }
            }
        }
    }

    public List<Boolean> getCorrects() {
        return corrects;
    }

    public void setCorrects(List<Boolean> corrects) {
        for (boolean correct : corrects) {
            if (correct) {
                ++correctCount;
            }
        }
        this.corrects = corrects;
    }
    
    @Override
    public String toString() {
        return ToStringUtils.objectToString(this);
    }
}
